package com.liu.mhl.View;


import com.liu.mhl.Server.BillServer;
import com.liu.mhl.Server.DiningTableServer;
import com.liu.mhl.Server.EmployeeServer;
import com.liu.mhl.Server.MenuServer;
import com.liu.mhl.domain.Bill;
import com.liu.mhl.domain.DiningTable;
import com.liu.mhl.domain.Employee;
import com.liu.mhl.domain.Menu;
import com.liu.mhl.utils.Utility;

import javax.jws.Oneway;
import java.util.List;

//主界面
public class MHLView {
    public static void main(String[] args) {
        new MHLView().mainMenu();
    }

    private boolean loop = true;
    private String key;//接收用户的输入
    //定义个员工Server对象
    private EmployeeServer employeeServer =  new EmployeeServer();
    //定义餐桌Server对象
    private DiningTableServer diningTableServer = new DiningTableServer();
    //定义菜品Server对象
    private MenuServer menuServer=  new MenuServer();
    //定义账单Server对象
     private BillServer billServer= new BillServer();

    //将员工信息显示
    public void showEmp(){
        List<Employee> employee = employeeServer.getEmployee();
        for(Employee e:employee){
            System.out.println(e);
        }
    }
     //完成结账
    public void payBill(){
        System.out.println("=============结账服务=============");
        System.out.println("请选择要结账的餐桌编号（-1退出）");
        int diningTableId = Utility.readInt();
        //验证是否退出
        if (diningTableId == -1) {
            System.out.println("退出服务");
            return;
        }

        //验证餐桌号是否存在
        DiningTable diningTableById = diningTableServer.getDiningTableById(diningTableId);
        if (diningTableById == null) {
            System.out.println("餐桌号错误");
            return;
        }
        //验证餐桌是否有结账的账单
        if(!billServer.hasBillByDiningTableId(diningTableId)){
            System.out.println("该餐位没有未结账的菜单");
            return;
        }

        System.out.println("请选择要结账的方式 现金/支付宝/微信回车退出）");
        String payMode = Utility.readString(20, "");
        if (payMode.equals(" ")) {
            System.out.println("============取消服务============");
            return;
        }
        //结账
        char key = Utility.readConfirmSelection();
        if (key == 'Y'){
            if(billServer.payBill(diningTableId,payMode)){
                System.out.println("============完成结账==========");
            }else{
                System.out.println("============结账失败==========");
            }
        }else{
            System.out.println("============取消服务============");
        }
    }

//显示账单
    public void listBill(){
        List<Bill> billList = billServer.list();
        System.out.println("\n编号\t\t菜品号\t\t菜品量\t\t金额\t\t桌号\t\t日期\t\t\t\t\t\t\t状态");
        for (Bill bill : billList) {
            System.out.println(bill);
        }
        System.out.println("显示完毕");
    }

    //完成点餐
    public void orderMenu(){
        System.out.println("===========点餐服务=========");
        System.out.println("输入点餐的桌号(-1退出)：");
        int orderDiningTableId = Utility.readInt();
        if (orderDiningTableId == -1) {
            System.out.println("===========取消点餐服务=========");
            return;
        }


        System.out.println("输入菜品的编号(-1退出)：");
        int orderMenuId = Utility.readInt();
        if (orderMenuId == -1) {
            System.out.println("===========取消点餐服务=========");
            return;
        }
        System.out.println("输入菜品的数量(-1退出)：");
        int orderNums = Utility.readInt();
        if (orderNums == -1) {
            System.out.println("===========取消点餐服务=========");
            return;
        }
        //检验餐桌号
        DiningTable diningTableById = diningTableServer.getDiningTableById(orderDiningTableId);
        if (diningTableById == null) {
            System.out.println("餐桌号不存在");
            return;
        }
        //检验菜品编号
        Menu menuById = menuServer.getMenuById(orderMenuId);
        if (menuById == null) {
            System.out.println("菜品号不存在");
            return;
        }
        //生成账单
        if(billServer.orderMenu(orderMenuId,orderNums,orderDiningTableId)){
            System.out.println("点餐成功");
        }else{
            System.out.println("点餐失败");
        }
    }
    //显示所有菜品
    public void  ShowAllDishes(){
        System.out.println("菜品编号\t\t菜品名\t\t类别\t\t价格");
        List<Menu> menus = menuServer.listMenu();
        for (Menu menu : menus) {
            System.out.println(menu);
        }
    }
    //完成订座
    public void orderDiningTable() {
        System.out.println("==========预订餐桌=========");
        System.out.println("请选择餐桌的编号（-1退出）：");
        int orderId = Utility.readInt(1);
        if (orderId == -1) {
            System.out.println("=========取消预订=========");
            return;
        }
        char key = Utility.readConfirmSelection();
        if (key == 'Y') {
            DiningTable diningTableById = diningTableServer.getDiningTableById(orderId);
            if (diningTableById == null) {
                System.out.println("=========预订餐桌不存在=========");
                return;
            }
            if (!("空".equals(diningTableById.getState()))){
                System.out.println("=========该餐桌已就餐或已预订=========");
                return;
            }
            System.out.println("预订人的名字：");
            String orderName = Utility.readString(50);
            System.out.println("预订人的电话：");
            String orderTel = Utility.readString(50);
             if(diningTableServer.orderDiningTable(orderId, orderName, orderTel)){
                 System.out.println("=========预订成功===========");
             }else {
                 System.out.println("=========预订失败===========");
             }

        }else{
            System.out.println("=========取消预订=========");
        }
    }
    //显示所有餐桌
    public void listDiningTable() {
        System.out.println("\n餐桌编号\t\t餐桌状态");
        List<DiningTable> list = diningTableServer.list();
        for (DiningTable diningTable : list) {
            System.out.println(diningTable);
        }
        System.out.println("============显示完毕============");
    }
    public void mainMenu() {
        while (loop) {
            System.out.println("============满汉楼登录页面=============");
            System.out.println("\t\t 1.登录");
            System.out.println("\t\t 2.退出");
            System.out.println("请输入你的选择：");
            key = Utility.readString(1);
            switch (key) {
                case "1":
                    //需要验证用户名和密码
                    System.out.println("请输入员工号：");
                    String empId = Utility.readString(50);
                    System.out.println("请输入密  码：");
                    String pwd = Utility.readString(50);
                    //去数据库验证 ，先做登录成功的验证
                    Employee employee  = employeeServer.getEmployeeByIdAndPwd(empId, pwd);
                    System.out.println("需要将全部员工信息显示吗（Y|N）");
                    char c = Utility.readConfirmSelection();
                    if (c == 'Y') {
                        showEmp();
                    }
                    if (employee != null) {
                        System.out.println("===============登录成功{"+employee.getName()+"}==========");
                        while (loop) {
                            //显示二级菜单
                            System.out.println("============满汉楼二级菜单===========");
                            System.out.println("\t\t 1.显示餐桌状态");
                            System.out.println("\t\t 2.预订餐桌");
                            System.out.println("\t\t 3.显示所有菜品");
                            System.out.println("\t\t 4.点餐服务");
                            System.out.println("\t\t 5.查看菜单");
                            System.out.println("\t\t 6.结账");
                            System.out.println("\t\t 9.退出满汉楼");
                            System.out.println("请输入你的选择:");
                            key = Utility.readString(1);
                            switch (key) {
                                case "1":
                                    listDiningTable();
                                    break;
                                case "2":
                                    orderDiningTable();
                                    break;
                                case "3":
                                    ShowAllDishes();
                                    break;
                                case "4":
                                    orderMenu();
                                    break;
                                case "5":
                                    listBill();
                                    break;
                                case "6":
                                    System.out.println("\t\t 6.结账");
                                    break;
                                case "9":
                                    System.out.println("\t\t 9.退出满汉楼");
                                    loop = false;
                                    break;
                                default:
                                    System.out.println("请重新输入;");
                                    break;
                            }
                        }
                    } else {
                        System.out.println("===============登录失败==========");
                    }
                    break;
                case "2":
                    loop = false;
                    break;
                default:
                    System.out.println("请正确输入：");
            }
        }
    }
}
